package com.yanwq.sayhi.controller.comment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanwq.sayhi.R;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.common.tool.DateFormatTool;
import com.yanwq.sayhi.controller.comment.activity.CommentListActivity;
import com.yanwq.sayhi.dao.bean.Comment;
import com.yanwq.sayhi.dao.bean.Reply;
import com.yanwq.sayhi.widget.InputDialog;

import java.util.List;

/**
 * 作者：yanweiqiang。
 * 日期：2016/6/15 0015。
 */
public class ReplyAdapter extends BaseAdapter {
    private List<Reply> mReplies;
    private Comment mComment;

    public ReplyAdapter(List<Reply> replies, Comment comment) {
        super();
        mReplies = replies;
        mComment = comment;
    }

    @Override
    public int getCount() {
        return mReplies.size();
    }

    @Override
    public Object getItem(int position) {
        return mReplies.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mReplies.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_reply,
                    parent,
                    false);
            holder.item = convertView.findViewById(R.id.reply_item);
            holder.avatar = (ImageView) convertView.findViewById(R.id.reply_avatar);
            holder.avatarTo = (ImageView) convertView.findViewById(R.id.reply_avatar_to);
            holder.nickname = (TextView) convertView.findViewById(R.id.reply_nickname);
            holder.nicknameTo = (TextView) convertView.findViewById(R.id.reply_nickname_to);
            holder.time = (TextView) convertView.findViewById(R.id.reply_time);
            holder.content = (TextView) convertView.findViewById(R.id.reply_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Reply reply = mReplies.get(position);

        MImageLoader.displayImage(reply.getFromUser().getAvatarThumbUrl(), holder.avatar);
        MImageLoader.displayImage(reply.getToUser().getAvatarThumbUrl(), holder.avatarTo);
        holder.nickname.setText(reply.getFromUser().getNickname());
        holder.nicknameTo.setText(reply.getToUser().getNickname());
        holder.time.setText(DateFormatTool.format(reply.getPublishTime()));
        holder.content.setText(reply.getContent());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final InputDialog inputDialog = new InputDialog(context);
                inputDialog.setPrompt(reply.getContent());
                inputDialog.setOk(context.getString(R.string.send));
                inputDialog.setCancel(context.getString(R.string.forget_it));
                inputDialog.setOkListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CommentListActivity) context).sendReply(inputDialog.getContent(), mComment, reply.getFromUser());
                    }
                });
                inputDialog.show();
            }
        });

        return convertView;
    }

    private class ViewHolder {
        View item;
        ImageView avatar;
        ImageView avatarTo;
        TextView nickname;
        TextView nicknameTo;
        TextView time;
        TextView content;
    }
}
